// import OpenAI from "openai";
import request from 'axios';

//let modelType = "deepseek-r1";
// let modelType="qwen-plus";
// let apikey = "sk-6fd128b3d0fe4cf4bc971b65a54267c9";
// const openai = new OpenAI(
//     {
//         // 若没有配置环境变量，请用百炼API Key将下行替换为：apiKey: "sk-xxx",
//         apiKey: apikey,
//         baseURL: "https://dashscope.aliyuncs.com/compatible-mode/v1",
//         dangerouslyAllowBrowser: true
//     }
// );
// export  const invokeAi = async (messageInfo,callback) => {
//     const response = await openai.chat.completions.create({
//         model: modelType,
//         messages: [
//             { role: "system", content: messageInfo },
//             { role: "user", content: messageInfo },
//             { role: "assistant", content: getStore({ name: 'assistantAiMessageInfo'}) == undefined ? "" : getStore({ name: 'assistantAiMessageInfo'}) }
//         ],
//         stream:true,
//     },{responseType:'stream',timeout:600000});
//     for await (const part of response) {
//         callback(part.choices[0].delta.content);
//     }
//     setStore({ name: 'assistantAiMessageInfo', content: messageInfo });
// }

// let modelType = "deepseek-r1:7b";
// const openai = new OpenAI(
//     {
//         apiKey: "111",
//         baseURL: "http://192.168.0.105:11434/v1",
//         dangerouslyAllowBrowser: true
//     }
// );


// export  const invokeAi = async (paramData,callback) => {
//     var messageInfo = paramData.messageInfo;
//     invokeMethod(modelType,messageInfo,callback);
// }

// export const invokeMethod = async(modelType,messageInfo,callback) =>{
//     const openai = new OpenAI(
//       {
//           apiKey: "application-5223774fdf72ff45d487663888ebe657",
//           baseURL: (import.meta.env.VITE_BASE_URL + "" + window.location.port + ""+ "/local-ai/chat/api/0198309f-0167-7773-9989-a7ab7f3b316c"),
//           dangerouslyAllowBrowser: true
//       }
//     );
//     const response = await openai.chat.completions.create({
//         model: modelType,
//         messages: [
//             // {
//             //     "role": "system",
//             //     "content":`用户将提供给你一段文字,请你分析内容,给出可能的国家医保诊断编码,诊断名称,治疗方案。输出格式：<div class="container"><div class="header"><h2>医疗诊断处方</h2><p>专业医疗建议与用药指导</p></div><div class="content"><div class="section"><div class="section-title">初步诊断</div><div class="diagnosis-item"><span>•诊断编码: <诊断编码> </span> <span>•诊断名称：<诊断名称> </span> <button class="btn-adopt" onclick=\"AI_UseDiag('<诊断编码>')\">采纳</button></div><div class="diagnosis-item"><span>•诊断编码: <诊断编码> </span> <span>•诊断名称：<诊断名称> </span>  <button class="btn-adopt" onclick=\"AI_UseDiag('<诊断编码>')\">采纳</button></div></div><div class="section"><div class="prescription-title">成药处方</div><div class="prescription-drug"><span class="drug-name"><药品名称></span><span class="drug-usage"><药品次用量><药品用法><药品频率> </span><span class="drug-notes"><药品说明></span></div></div></div></div>`
//             // },
//             { role: "user", content: messageInfo+",请你分析内容,从知识库给出可能的国家医保诊断编码,诊断名称,药品处方建议。"}
//             // { role: "user", content: messageInfo+`,根据患者年龄,性别和主诉,给出可能的国家医保诊断编码,诊断名称,治疗方案。输出格式：<div class="container"><div class="header"><h2>医疗诊断处方</h2><p>专业医疗建议与用药指导</p></div><div class="content"><div class="section"><div class="section-title">初步诊断</div><div class="diagnosis-item"><span>•诊断编码: <诊断编码> </span> <span>•诊断名称：<诊断名称> </span> <button class="btn-adopt" onclick=\"AI_UseDiag('诊断编码')\">采纳</button></div><div class="diagnosis-item"><span>•诊断编码: <诊断编码> </span> <span>•诊断名称：<诊断名称> </span>  <button class="btn-adopt" onclick=\"AI_UseDiag('诊断编码')\">采纳</button></div></div><div class="section"><div class="prescription-title">成药处方</div><div class="prescription-drug"><span class="drug-name"><药品名称></span><span class="drug-usage"><药品次用量><药品用法><药品频率> </span><span class="drug-notes"><药品说明></span></div></div></div></div>`}
//         ],
//         extra_body:{
//           "chat_id":"019830c1-e4bf-75f0-bf2a-b6d6d2cb4a32"
//         },
//         stream:true,
//     },{responseType:'stream',timeout:600000});
//     var start = false;
//     for await (const part of response) {
//         if(!start){
//           callback(`<style>

//             .container {
//               width: 100%;
//               max-width: 800px;
//               background-color: var(--card-bg);
//               border-radius: 12px;
//               box-shadow: 0 10px 30px rgba(0, 0, 0, 0.05);
//               overflow: hidden;
//               transition: all 0.3s ease;

//               font-family: "Microsoft YaHei", sans-serif;
//               line-height: 1.6;
//               color: var(--text-color);
//               padding: 2rem 1rem;
//               justify-content: center;
//               margin: 0;
//               padding: 0;
//               box-sizing: border-box;
//               --primary-color: #16a085;
//               --secondary-color: #27ae60;
//               --accent-color: #e74c3c;
//               --text-color: #2c3e50;
//               --light-text: #7f8c8d;
//               --background: #f9f9f9;
//               --card-bg: #ffffff;
//               --border-color: #eaeaea;
//             }
        
//             .container:hover {
//               box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
//             }
        
//             .header {
//               padding: 1.5rem 2rem;
//               background: linear-gradient(135deg, var(--primary-color) 0%, #138a72 100%);
//               color: white;
//               position: relative;
//               overflow: hidden;
//             }
        
//             .header::before {
//               content: "";
//               position: absolute;
//               top: 0;
//               right: 0;
//               width: 100%;
//               height: 100%;
//               background-image: url("data:image/svg+xml,%3Csvg width='100' height='100' viewBox='0 0 100 100' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M11 18c3.866 0 7-3.134 7-7s-3.134-7-7-7-7 3.134-7 7 3.134 7 7 7zm48 25c3.866 0 7-3.134 7-7s-3.134-7-7-7-7 3.134-7 7 3.134 7 7 7zm-43-7c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zm63 31c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zM34 90c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zm56-76c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zM12 86c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm28-65c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm23-11c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zm-6 60c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm29 22c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zM32 63c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zm57-13c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zm-9-21c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2zM60 91c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2zM35 41c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2zM12 60c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2z' fill='%23ffffff' fill-opacity='0.05' fill-rule='evenodd'/%3E%3C/svg%3E");
//               opacity: 0.1;
//               pointer-events: none;
//             }
        
//             .header h2 {
//               font-size: 1.75rem;
//               margin-bottom: 0.5rem;
//               font-weight: 600;
//               text-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
//               color: white !important;
//             }
        
//             .header p {
//               font-size: 0.9rem;
//               opacity: 0.9;
//             }
        
//             .content {
//               padding: 2rem;
//             }
        
//             .section {
//               margin-bottom: 2.5rem;
//               position: relative;
//             }
        
//             .section::after {
//               content: "";
//               display: block;
//               height: 1px;
//               width: 100%;
//               background-color: var(--border-color);
//               margin-top: 1.5rem;
//             }
        
//             .section:last-child::after {
//               display: none;
//             }
        
//             .section-title {
//               font-size: 1.25rem;
//               font-weight: 600;
//               margin-bottom: 1.25rem;
//               color: var(--text-color);
//               display: flex;
//               align-items: center;
//             }
        
//             .section-title::before {
//               content: "";
//               width: 4px;
//               height: 20px;
//               background-color: var(--primary-color);
//               border-radius: 2px;
//               margin-right: 0.75rem;
//             }
        
//             .diagnosis-item {
//               padding: 0.75rem 1rem;
//               margin-bottom: 0.75rem;
//               border-radius: 8px;
//               background-color: var(--background);
//               display: flex;
//               align-items: center;
//               justify-content: space-between;
//               transition: all 0.2s ease;
//               border-left: 3px solid transparent;
//             }
        
//             .diagnosis-item:hover {
//               background-color: #f0f2f5;
//               transform: translateX(2px);
//             }
        
//             .diagnosis-item.selected {
//               border-left-color: var(--primary-color);
//               background-color: rgba(22, 160, 133, 0.05);
//             }
        
//             .diagnosis-item span {
//               font-size: 1rem;
//               flex-grow: 1;
//             }
        
//             .btn-adopt {
//               background: linear-gradient(135deg, var(--primary-color) 0%, #138a72 100%);
//               color: white;
//               border: none;
//               padding: 0.5rem 1.25rem;
//               border-radius: 6px;
//               cursor: pointer;
//               font-size: 0.9rem;
//               font-weight: 500;
//               transition: all 0.2s ease;
//               box-shadow: 0 2px 5px rgba(22, 160, 133, 0.2);
//               display: inline-flex;
//               align-items: center;
//             }
        
//             .btn-adopt:hover {
//               box-shadow: 0 4px 10px rgba(22, 160, 133, 0.3);
//               transform: translateY(-1px);
//             }
        
//             .btn-adopt:active {
//               transform: translateY(0);
//               box-shadow: 0 1px 3px rgba(22, 160, 133, 0.2);
//             }
        
//             .btn-adopt:disabled {
//               background: #bdc3c7;
//               cursor: not-allowed;
//               box-shadow: none;
//             }
        
//             .prescription-title {
//               font-size: 1.15rem;
//               font-weight: 600;
//               margin-bottom: 1rem;
//               color: var(--text-color);
//               display: flex;
//               align-items: center;
//             }
        
//             .prescription-title::before {
//               content: "💊";
//               margin-right: 0.5rem;
//               font-size: 1.2rem;
//             }
        
//             .prescription-drug {
//               padding: 1rem;
//               margin-bottom: 1.25rem;
//               border-radius: 8px;
//               background-color: var(--background);
//               border-left: 4px solid var(--secondary-color);
//               transition: all 0.3s ease;
//               position: relative;
//               overflow: hidden;
//             }
        
//             .prescription-drug:hover {
//               box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
//               transform: translateY(-2px);
//             }
        
//             .prescription-drug::before {
//               content: "";
//               position: absolute;
//               top: 0;
//               right: 0;
//               width: 100%;
//               height: 100%;
//               background-image: url("data:image/svg+xml,%3Csvg width='100' height='100' viewBox='0 0 100 100' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M11 18c3.866 0 7-3.134 7-7s-3.134-7-7-7-7 3.134-7 7 3.134 7 7 7zm48 25c3.866 0 7-3.134 7-7s-3.134-7-7-7-7 3.134-7 7 3.134 7 7 7zm-43-7c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zm63 31c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zM34 90c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zm56-76c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zM12 86c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm28-65c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm23-11c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zm-6 60c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm29 22c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zM32 63c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zm57-13c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zm-9-21c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2zM60 91c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2zM35 41c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2zM12 60c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2z' fill='%23000000' fill-opacity='0.01' fill-rule='evenodd'/%3E%3C/svg%3E");
//               opacity: 0.05;
//               pointer-events: none;
//             }
        
//             .prescription-drug span {
//               display: block;
//               margin-bottom: 0.3rem;
//             }
        
//             .drug-name {
//               font-size: 1.05rem;
//               font-weight: 600;
//               color: var(--text-color);
//             }
        
//             .drug-usage {
//               font-size: 0.9rem;
//               color: var(--light-text);
//             }
        
//             .drug-notes {
//               font-size: 0.9rem;
//               color: var(--text-color);
//               margin-top: 0.5rem;
//               padding-top: 0.5rem;
//               border-top: 1px dashed var(--border-color);
//             }
        
//             /* 响应式设计 */
//             @media (max-width: 576px) {
//               .container {
//                 margin: 0;
//                 border-radius: 0;
//               }
        
//               .content {
//                 padding: 1.5rem;
//               }
        
//               .diagnosis-item {
//                 flex-direction: column;
//                 align-items: flex-start;
//               }
        
//               .btn-adopt {
//                 margin-top: 0.75rem;
//                 width: 100%;
//               }
//             }
        
//             /* 动画效果 */
//             @keyframes fadeIn {
//               from { opacity: 0; transform: translateY(10px); }
//               to { opacity: 1; transform: translateY(0); }
//             }
        
//             .diagnosis-item, .prescription-drug {
//               animation: fadeIn 0.3s ease forwards;
//               animation-delay: calc(var(--i) * 0.05s);
//               opacity: 0;
//             }
        
//             .diagnosis-item {
//               --i: 1;
//             }
        
//             .diagnosis-item:nth-child(2) {
//               --i: 2;
//             }
        
//             .prescription-drug {
//               --i: 3;
//             }
//           </style>`);
//           start = true;
//         }
//         // if(start){
//         //     callback(part.choices[0].delta.content);
//         // }
//         // if("</think>" == part.choices[0].delta.content){
//         //     start = true;
//         // }
//         callback(part.choices[0].delta.content);
//     }
// }

export  const invokeAi = async (paramData,callback) => {
  var type = paramData.type;
  var messageInfo = paramData.messageInfo;
  //根据主诉获取诊断
  if(type == "getDiagByZs"){
    await request({
      method: "post",
      url: '/cloudhis-ai/getDiagByZs',
      data: {
        "inputs": messageInfo,
        "response_mode":"blocking",
        "user": paramData.user
      }
    }).then((response) => {
      paramData.success = true;
      callback(response.data.data.outputs,paramData);
    }).catch(error => {
      paramData.success = false;
      callback('请求失败:'+error,paramData);
    });
  }
  //检查检验信息
  else if(type == "getJcJyByZs"){
    await request({
      method: "post",
      url: '/cloudhis-ai/getJcJyByZs',
      data: {
        "query": messageInfo,
        "response_mode":"blocking",
        "user": paramData.user
      }
    }).then((response) => {
      paramData.success = true;
      callback(response.data.answer,paramData);
    }).catch(error => {
      paramData.success = false;
      callback('请求失败:'+error,paramData);
    });
  }
  //处方信息
  else if(type == "getPresByInfo"){
    await request({
      method: "post",
      url: '/cloudhis-ai/getPresByInfo',
      data:  messageInfo,
      timeout:1000*60*5, //5分钟
    }).then((response) => {
      paramData.success = true;
      callback(response.data.answer,paramData);
    }).catch(error => {
      paramData.success = false;
      callback('请求失败:'+error,paramData);
    });
  }
  //AI审方
  else if(type == "getCheckPresInfo"){
    await request({
      method: "post",
      url: '/cloudhis-ai/getCheckPresInfo',
      timeout:1000*60*5, //5分钟
      data: {
        "query": messageInfo,
        "response_mode":"blocking",
        "user": paramData.user
      }
    }).then((response) => {
      paramData.success = true;
      callback(response.data.answer,paramData);
    }).catch(error => {
      paramData.success = false;
      callback('请求失败:'+error,paramData);
    });
  }
  //知识库检索
  else if(type == "getKnowledgeRetrieve"){
    await request({
      url: '/cloudhis-ai/getKnowledgeRetrieve',
      data: messageInfo,
      method: 'post',
    }).then((response) => {
      paramData.success = true;
      callback(response,paramData);
    }).catch(error => {
      paramData.success = false;
      callback('请求失败:'+error,paramData);
    });
  }
  //获取语音病历信息
  else if(type == "getVoice"){
    await request({
      url: '/cloudhis-ai/getVoice',
       headers: {
        'Content-Type': "multipart/form-data",
      },
      data: paramData.formData,
      method: 'post',
    }).then((response) => {
      paramData.success = true;
      callback(response.data.data.outputs,paramData);
    }).catch(error => {
      paramData.success = false;
      callback('请求失败:'+error,paramData);
    });
  }
}


export const useDiag = (mzDiagnosis) => {
  return request({
    url: '/cloudhis-outsys/ai/useDiag',
    method: 'post',
    data: mzDiagnosis
  })
}